﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;


namespace PRE.Catalog
{
    public partial class FrmBorrowersInfo : DevExpress.XtraEditors.XtraForm
    {
        #region Variable

        DAL.EntitiesDAL.TheDocGiaDAL _dal = new DAL.EntitiesDAL.TheDocGiaDAL();
        BLL.EntitiesBLL.TheDocGiaBLL _bll = new BLL.EntitiesBLL.TheDocGiaBLL();

        DataTable _dtdata = new DataTable();

        #endregion

        #region Init

        #region FrmBorrowersInfo
        public FrmBorrowersInfo()
        {
            InitializeComponent();
        }
        #endregion

        #region FrmBorrowersInfo

        #endregion

        #endregion

        #region Functions
        private void FrmBorrowersInfo_Load(object sender, EventArgs e)
        {
            if (radioGroupLoaiTraCuu.SelectedIndex == 0)
            {
                layoutControlItem_TenDocGia.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }
        #endregion

        #region Events

        #region private void radioGroupLoaiTraCuu_SelectedIndexChanged(object sender, EventArgs e)
        private void radioGroupLoaiTraCuu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroupLoaiTraCuu.SelectedIndex == 0)
            {
                gridControl_TheDocGia.DataSource = null;
                layoutControlItem_MaDocGia.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                layoutControlItem_TenDocGia.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
            else
            {
                gridControl_TheDocGia.DataSource = null;
                layoutControlItem_TenDocGia.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                layoutControlItem_MaDocGia.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }

        }
        #endregion

        #region private void textEdit_MaDocGia_KeyDown(object sender, KeyEventArgs e)
        private void textEdit_MaDocGia_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.Enter)
                {
                    if (textEdit_MaDocGia.Text == string.Empty)
                    {
                        XtraMessageBox.Show("Chưa nhập mã đọc giả", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    _dtdata = _dal.Search_MaThedocgia(textEdit_MaDocGia.Text);

                    if (_dtdata.Rows.Count == 0)
                    {
                        gridControl_TheDocGia.DataSource = null;
                        XtraMessageBox.Show("Không tìm thấy đọc giả. Kiểm tra lại mã đọc giả.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    foreach (DataColumn dc in _dtdata.Columns)
                        dc.ReadOnly = true;

                    _dtdata.Columns.Add("IsCheck", typeof(bool));
                    _dtdata.Columns["IsCheck"].ReadOnly = false;
                    foreach (DataRow dr in _dtdata.Rows)
                    {
                        dr["IsCheck"] = false;
                    }

                    gridControl_TheDocGia.DataSource = _dtdata;

                    try
                    {
                        AppGridView.SummaryField(gridView_TheDocGia, "MADOCGIA", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);

                        gridView_TheDocGia.OptionsView.EnableAppearanceEvenRow = true;
                        gridView_TheDocGia.OptionsView.ColumnAutoWidth = true;
                        gridView_TheDocGia.BestFitColumns();
                    }
                    catch { }
                }
            }
            catch { }
        }
        #endregion

        #region private void textEdit_TenDocGia_KeyDown(object sender, KeyEventArgs e)
        private void textEdit_TenDocGia_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.Enter)
                {
                    if (textEdit_TenDocGia.Text == string.Empty)
                    {
                        XtraMessageBox.Show("Chưa nhập tên đọc giả", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    _dtdata = _dal.Search_TenThedocgia(textEdit_TenDocGia.Text);

                    if (_dtdata.Rows.Count == 0)
                    {
                        gridControl_TheDocGia.DataSource = null;
                        XtraMessageBox.Show("Không tìm thấy đọc giả. Kiểm tra lại tên đọc giả.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    foreach (DataColumn dc in _dtdata.Columns)
                        dc.ReadOnly = true;

                    _dtdata.Columns.Add("IsCheck", typeof(bool));
                    _dtdata.Columns["IsCheck"].ReadOnly = false;
                    foreach (DataRow dr in _dtdata.Rows)
                    {
                        dr["IsCheck"] = false;
                    }

                    gridControl_TheDocGia.DataSource = _dtdata;

                    try
                    {
                        AppGridView.SummaryField(gridView_TheDocGia, "MADOCGIA", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);


                        gridView_TheDocGia.OptionsView.EnableAppearanceEvenRow = true;
                        gridView_TheDocGia.OptionsView.ColumnAutoWidth = true;
                        gridView_TheDocGia.BestFitColumns();
                    }
                    catch { }
                }
            }
            catch { }
        }
        #endregion

        #region private void checkEdit_ChonTatCa_CheckedChanged(object sender, EventArgs e)
        private void checkEdit_ChonTatCa_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gridView_TheDocGia.DataRowCount; i++)
            {
                gridView_TheDocGia.GetDataRow(i)["IsCheck"] = checkEdit_ChonTatCa.Checked;
            }
        }
        #endregion

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion


    }
}
